package client;

import java.util.Scanner;


public class GUIThread extends Thread {
	private Monitor monitor;
	private String dataString;
	private int cmd;
	private Scanner in;
	private final String MENU = 
			"Commands:\n" +
			"1 = Fetch document list\n" +
			"2 = Open (and fetch) document\n" +
			"3 = Fetch document\n" +
			"4 = Add to document\n" +
			"5 = Close document\n" +
			"6 = Create document\n" +
			"7 = Delete document\n" +
			"8 = ...\n" +
			"9 = ...\n" +
			"0 = Menu";
	
	/**
	 * Starts the GUI
	 * 
	 * @param monitor
	 *            the client back-end monitor
	 */
	public GUIThread(Monitor monitor) {
		this.monitor = monitor;
		in = new Scanner(System.in);
		cmd = 0;
		dataString = "";
	}

	/**
	 * Calculates fps and displays the images.
	 */
	public void run() {
		while (true) {
			while (cmd == 8 || cmd == 9 || cmd == 0){
				System.out.println(MENU);
				input();
			}
		monitor.setSendData(cmd, dataString);
		String output = monitor.getDisplayMessage();
		System.out.println(output);
		input();
		}
	}
	
	private void input(){
		dataString = "";
		String nextLine;

		System.out.println("Print cmd:");
		cmd=in.nextInt();
		cmd%=10;
		
		switch(cmd){
		case 2:
		case 3:
		case 4:
		case 6:
		case 7:
			System.out.println("Print message:");
			do {
				nextLine = in.nextLine();
				if (nextLine.length() > 0) dataString += nextLine + "\n";
			} while (nextLine.length() != 0 || dataString.length() < 3);
			dataString = dataString.substring(0, dataString.length()-1);
			break;
		default:
			dataString = "EOF";
			break;
		}
	}
}
